import ftxClient
import timeUtil
import time
from datetime import datetime, timedelta
from ciso8601 import parse_datetime

if __name__ == '__main__':
    timeUtil = timeUtil.TimeUtil()
    client = ftxClient.FtxClient('AUKAE1uTGtAwho6c3zsCVB311jZw3qVdrJ7UR35Q', 'gYwknqMSecSCv7tnmpXe-FviBNMUaeVtOBR-HjMf')

    # startTime = timeUtil.get_time_stamp(timeUtil.local_2_utc_str(timeUtil.get_cur_date_date()))
    # endTime = timeUtil.get_time_stamp(timeUtil.local_2_utc_str(timeUtil.get_next_date_date()))
    startTime = timeUtil.get_time_stamp_date(timeUtil.get_cur_date_str())
    endTime = timeUtil.get_time_stamp_date(timeUtil.get_next_date_str())
    # startTime = timeUtil.get_time_stamp_date('2021-04-09')
    # endTime = timeUtil.get_time_stamp_date('2021-04-10')
    print(startTime)
    print(endTime)
    fundingPayments = client.get_funding_payments(start_time=startTime, end_time=endTime)
    # fundingPayments = client.get_funding_payments()
    print(len(fundingPayments))
    fundingPayments.sort(key=lambda k: (parse_datetime(k.get('time')).strftime('%Y-%m-%d %H:%M:%S'), 0))

    print("sum_payment========================================================================")
    sum_payment = 0
    for i in range(len(fundingPayments)):
        # print(fundingPayments[i])
        # print(timeUtil.utc_2_pk(parse_datetime(fundingPayments[i]['time']).strftime('%Y-%m-%d %H:%M:%S')))
        print('index:%s,id:%s,future:%s,datetime:%s,payment:%s'
              % (i, fundingPayments[i]['id'], fundingPayments[i]['future'],
                 timeUtil.utc_2_pk(parse_datetime(fundingPayments[i]['time']).strftime('%Y-%m-%d %H:%M:%S')),
                 fundingPayments[i]['payment']))
        # if fundingPayments[i]['future'] != 'future' and parse_datetime(fundingPayments[i]['time']).strftime(
        #         '%Y-%m-%d') == '2021-04-10':
        #     print(parse_datetime(fundingPayments[i]['time']).strftime('%Y-%m-%d %H:%M:%S'))
        sum_payment = sum_payment + fundingPayments[i]['payment'];

    print(sum_payment)
    print("========================================================================")
